package com.coolpad.job.device.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;

/**
 * 功能描述:<br/>
 *
 * @Author:shengtao_yang
 * @Date: 2020/12/21 13:26
 * @since: 1.0.0
 */
@Slf4j
public class JsonUtils {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        //设置实体无属性和json串属性对应时不会出错，只转换对应的属性
        MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        // 忽略空bean
        MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        // 允许单引号
        MAPPER.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    }

    /**
     * 将对象转换成json字符串。
     *
     * @param data
     * @return
     */
    public static String beanToJson(Object data) {
        try {
            return MAPPER.writeValueAsString(data);
        } catch (JsonMappingException e) {
            log.error("",e);
        } catch (IOException e) {
            log.error("",e);
        }
        return null;
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型，<T> 将此方法声明为泛型方法，可传入任何对象
     * @return <T> 目标对象
     */
    public static <T> T jsonToBean(String jsonData, Class<T> beanType) {
        if (StringUtils.isEmpty(jsonData) || null == beanType) {
            return null;
        }
        try {
            return MAPPER.readValue(jsonData, beanType);
        } catch (Exception e) {
            log.error("",e);
        }
        return null;
    }

    /**
     * 将json数据转换成pojo对象list
     *
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        if (StringUtils.isEmpty(jsonData) || null == beanType) {
            return null;
        }
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            return MAPPER.readValue(jsonData, javaType);
        } catch (Exception e) {
            log.error("json解析失败，json:{}",jsonData,e);
        }
        return null;
    }

    public static JsonNode stringToNode(String jsonString){

        JsonNode node = null;
        try {
            node = MAPPER.readTree(jsonString);
            return node;
        } catch (IOException e) {
            log.error("to jsonNode exception ：{}",node);
            throw new RuntimeException("string to jsonNode exception");
        }
    }

    public static void main(String[] args) throws Exception {
        String s = "{\n" +
                "    'imei1': '867305032765328',\n" +
                "    'update_ts': 1606293313284,\n" +
                "    'model': 'COOLPAD X1',\n" +
                "    'device_info': {\n" +
                "        'imei1': '867305032765328',\n" +
                "        'model': 'COOLPAD X1',\n" +
                "        'imei2': '867305032765442',\n" +
                "        'imsi': '8643329304532346',\n" +
                "        'radio': 'mobile-lte-3gnet',\n" +
                "        'mac_addr': '00-1c-25-d0-45-33',\n" +
                "        'bluetooth_addr': '24-09-81-33-24-11',\n" +
                "        'sn': '3125832991',\n" +
                "        'android_id': 'ae74841a4cff583c',\n" +
                "        'phone_number': '13329918775',\n" +
                "        'gps': '179.9999#41.79452',\n" +
                "        'ip_addr': '192.168.1.1',\n" +
                "        'rom': '63.5',\n" +
                "        'ram': '4',\n" +
                "        'available_rom': '33.3',\n" +
                "        'user_account': 'beautify_pad',\n" +
                "        'os_version': '9-9.9.24',\n" +
                "        'android_version': '11',\n" +
                "        'board': 'sdm845',\n" +
                "        'brand': 'COOLPAD',\n" +
                "        'region':'cn',\n" +
                "        'locale':'chinese_cn',\n" +
                "        'update_ts': 1605785408491\n" +
                "    }\n" +
                "}";
        JsonNode jsonNode = MAPPER.readTree(s);
        System.out.println(123);
    }

}
